$(function() {
	ajax();
	select();
	
		//修改产品开始
		$('#product-update').click(function() {
		
			//1.进行产品价格校验开始
			var priceValue = $('input[name="productPrice"]').val();
			var reg = /^(([1-9][0-9]*)|(([0]\.\d{1,2}|[1-9][0-9]*\.\d{1,2})))$/;
			var flag = reg.test(priceValue);
			if(flag == false) {
				$(this).removeAttr('data-dismiss');
				$('#error-message').attr('class', 'alert alert-danger alert-dismissable');
				$('#error-message').text("价格必须是数字...");
				$('#error').modal('show');
				popHide("error");
				return false;
			}
			//1.进行产品价格校验结束
		
			//2.进行产品重量校验开始
			var weightValue = $('input[name="productWeight"]').val();
			var reg = /^[0-9]{1,}$/;
			var flag = reg.test(weightValue);
			if(flag == false) {
				$(this).removeAttr('data-dismiss');
				$('#error-message').attr('class', 'alert alert-danger alert-dismissable');
				$('#error-message').text('重量必须是数字...');
				$('#error').modal('show');
				popHide("error");
				return false;
			}
			//2.进行产品重量校验结束
		
			//3.进行传输参数到后台开始
			$.ajax({
				type: "post",
				url: getPath() + "/product",
				data: ({
					"method": "updateProductMessage",
					"id":$('#update-hidden').val(),
					"companyId": $('#companyId option:selected').attr('value'),
					"productPrice": $('input[name="productPrice"]').val(),
					"productWeight": $('input[name="productWeight"]').val(),
					"productName": $('input[name="productName"]').val(),
				}),
				dataType: "json",
				success: function(jsonData) {
					if(jsonData.result == 'success') {
						$('#success-message').html(jsonData.message);
						$('#success-message').attr('class', 'alert alert-success alert-dismissable');
						$('#success').modal('show');
						goQueryPage('product-list');
					}else{
						$('#error-message').attr('class', 'alert alert-danger alert-dismissable');
						$('#error-message').html(jsonData.message);
						$('#error').modal('show');
						popHide("error");
					}
				}
			});
		});
		//修改产品结束
	
		//反选的模块开始
		$('#check-all').bind('click', function() {
			var list = $('input[type="checkbox"]');
			list.each(function(index, data) {
				if(index != 0) {
					if(data.checked) {
						data.checked = false;
					} else {
						data.checked = true;
					}
				}
			});
		});
		//反选的模块结束
	
		//删除产品开始
		$('#open-product-delete').click('click', function() {
	
			//1.获取到需要删除的id开始
			var checkedList = $('input[type="checkbox"]:checked');
			var proIds = "";
			for(var i = 0; i < checkedList.length; i++) {
				if(checkedList[i].value != 'on') {
					proIds = proIds + checkedList[i].value + ",";
				}
			}
			proIds = proIds.substring(0, proIds.length - 1);
			//1.获取到需要删除的id结束
	
			$.ajax({
				type: "post",
				url: getPath() + "/product",
				data: ({
					"method": "batchDeleteIds",
					"proIds": proIds,
				}),
				dataType: "json",
				success: function(jsonData) {
					if(jsonData.result == 'success') {
						$('#success-message').html(jsonData.message);
						$('#success-message').attr('class', 'alert alert-success alert-dismissable');
						$('#success').modal('show');
						goQueryPage('product-list');
					} else {
						$('#error-message').attr('class', 'alert alert-danger alert-dismissable');
						$('#error-message').html(jsonData.message);
						$('#error').modal('show');
						popHide("error");
					}
				}
			});
		});
		//删除产品结束
	
		//增加产品开始
		$('#product-add').click(function() {
	
			//1.进行产品价格校验开始
			var priceValue = $('input[name="productPrice"]').val();
			var reg = /^(([1-9][0-9]*)|(([0]\.\d{1,2}|[1-9][0-9]*\.\d{1,2})))$/;
			var flag = reg.test(priceValue);
			if(flag == false) {
				$(this).removeAttr('data-dismiss');
				$('#error-message').attr('class', 'alert alert-danger alert-dismissable');
				$('#error-message').text("价格必须是数字...");
				$('#error').modal('show');
				popHide("error");
				return false;
			}
			//1.进行产品价格校验结束
	
			//2.进行产品重量校验开始
			var weightValue = $('input[name="productWeight"]').val();
			var reg = /^[0-9]{1,}$/;
			var flag = reg.test(weightValue);
			if(flag == false) {
				$(this).removeAttr('data-dismiss');
				$('#error-message').attr('class', 'alert alert-danger alert-dismissable');
				$('#error-message').text('重量必须是数字...');
				$('#error').modal('show');
				popHide("error");
				return false;
			}
			//2.进行产品重量校验结束
	
			//3.进行传输参数到后台开始
			$.ajax({
				type: "post",
				url: getPath() + "/product",
				data: ({
					"method": "add",
					"companyId": $('#companyId option:selected').attr('value'),
					"productPrice": $('input[name="productPrice"]').val(),
					"productWeight": $('input[name="productWeight"]').val(),
					"productName": $('input[name="productName"]').val(),
				}),
				dataType: "json",
				success: function(jsonData) {
					if(jsonData.result == 'success') {
						$('#success-message').html(jsonData.message);
						$('#success-message').attr('class', 'alert alert-success alert-dismissable');
						$('#success').modal('show');
						goQueryPage('product-list');
					} else {
						$('#error-message').attr('class', 'alert alert-danger alert-dismissable');
						$('#error-message').html(jsonData.message);
						$('#error').modal('show');
						popHide("error");
					}
				}
			});
			//3.进行传输参数到后台结束
		});
		//增加产品结束
	
		//关闭模态框调回查询页面开始
		$('#product-add-modal,#product-update-modal').on('hide.bs.modal', function() {
			goQueryPage('over-modal');
		});
		//关闭模态框调回查询页面结束
});

//公司生成下啦菜单开始
function select() {
	$('#companyId').empty();
	$.ajax({
		type: "post",
		url: getPath() + "/company",
		data: ({
			"method": "findAll",
		}),
		dataType: "json",
		success: function(jsonData) {
			$(jsonData.data).each(function(index, data) {
				var company = $('<option value=' + data.id + '>' + data.companyName + '</td>');
				$('#companyId').append(company);
			});
		}
	});
}
//公司生成下啦菜单结束

//查询全部产品数据开始
function ajax() {
	$('#tbody').empty();
	$.ajax({
		type: "post",
		url: getPath() + "/product",
		data: ({
			"method": "findAll",
		}),
		dataType: "json",
		success: function(jsonData) {
			$(jsonData.data).each(function(index, data) {
				var tr = $('<tr class="info"></tr>');
				var check = $('<td><input type="checkbox" value="' + data.id + '" /></td>');
				var productName = $('<td>' + data.productName + '</td>');
				//var sortName 	  	= $('<td>' + data.sortName + '</td>');
				var productPrice = $('<td>' + data.productPrice + '</td>');
				var productWeight = $('<td>' + data.productWeight + '</td>');
				var companyName = $('<td>' + data.companyName + '</td>');
				var companyManager = $('<td>' + data.companyManager + '</td>');

				tr.append(check);
				tr.append(productName);
				//tr.append(sortName);
				tr.append(productPrice);
				tr.append(productWeight);
				tr.append(companyName);
				tr.append(companyManager);

				$('#tbody').append(tr);
			});
		}
	});
}
//查询全部产品数据结束

//自动关闭窗口开始
function popHide(object) {
	setTimeout(function() {
		$('#' + object).modal('hide');
	}, 1500);
}
//自动关闭窗口结束

//跳转到查询页面开始
function goQueryPage(page) {
	if(page === "over-modal"){
		ajax();
		window.location.href =  "product-list.html";
		return;
	}
	
	//$('#product-add-modal,#product-update-modal').modal('hide');
	setTimeout(function() {
		popHide("success");
		ajax();
		window.location.href = page + ".html";
	}, 2000);
}
//跳转到查询页面结束